//import pathToRegexp from 'path-to-regexp';
import { loginAPi,
    quickLoginAPi,
    UserProtocol,
    verifyAPi,regAPi,
    regVerifyAPi,memberPwdAPi,
    payPwdAPi,goCartAPi ,
    getRecoverApi,
    postMobileApi,
    codeToken,
    mobileApi,
    getMobileVerifyApi
} from './api';
import { Toast } from 'antd-mobile';
import {goodsDetailApi} from "../goodsDetail/api";
//import { routerRedux } from 'dva/router';

export default {
  namespace: 'login',
  state: {
      ret:[],
      Ret:[],
      UserProtocol:[]
  },
  reducers: {
    save(state, { data }) {
      return { ...state, ...data };
    },
  },
  effects: {
    *loginEFF({ obj }, { put, call }) {
      Toast.loading('加载中...', 3);
      const result = yield call(loginAPi, obj);
       if (result.code == 200) {
           yield call(goCartAPi)
            //Toast.hide();
            // yield put({ type:'save', data:result.data[0] })
            // Toast.success(result.msg, 1.5, ()=>{
            //     dispatch(routerRedux.replace({
            //         pathname: '/',
            //     }))
            // });
            // resolve(result);
            // 登录成功保存 token
            //localStorage.setItem('token', result.data[0].token);
        // } else {
        //     Toast.fail(result.msg, 1.5);
        }
      return result
    },
      *quickLoginEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(quickLoginAPi, obj);
          return result
      },
      *codeTokenEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(codeToken, obj);
          return result
      },
      *RegEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(regAPi, obj);
          return result
      },
      *MemberPwdEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(memberPwdAPi, obj);
          return result
      },
      *PayPwdEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(payPwdAPi, obj);
          return result
      },
      *verifyEFF({obj},{put,call}){
          Toast.loading('加载中...', 3);
          const result = yield call(verifyAPi, obj);
          if (result.code == 200) {
              yield put({ type: 'save', data: {ret:result.ret||[]} });
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5,()=>{
                  // this.props.dispatch(routerRedux.push({
                  //     pathname: `/login`,
                  // }));
              });
          }
          return result;
      },
      *regVerifyEFF({obj},{put,call}){
        console.log(obj)
          Toast.loading('加载中...', 3);
          const result = yield call(regVerifyAPi, obj);
          if (result.code == 200) {
              yield put({ type: 'save', data: {Ret:result.ret||[]} });
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5,()=>{
                  // this.props.dispatch(routerRedux.push({
                  //     pathname: `/login`,
                  // }));
              });
          }
          return result;
      },
      *cartEFF({ },{put,call}){
          const result = yield call(goCartAPi);
          console.log('1111')
          if (result.code == 200) {
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5,()=>{
              });
          }
      },

      *UserProtocolEFF({  }, { put, call }) {
          const result = yield call(UserProtocol);
          if (result.code == 200) {
              yield put({ type: 'save', data: { UserProtocol: result.ret||[] } });
              // Toast.success(result.msg, 1.5);
          } else {
              Toast.fail(result.msg, 1.5);
          }
      },
      // 忘记密码。。。
      *recoverEFF({
                      obj
                  }, {put, call}) {
          Toast.loading('加载中...', 3);
          const result = yield call(getRecoverApi, obj);
          if (result.code == 200) {
              // yield put({ type: 'save', data: {Ret:result.ret||[]} });
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5, () => {
                  // this.props.dispatch(routerRedux.push({
                  //     pathname: `/login`,
                  // }));
              });
          }

          return result;
      },

      // 忘记密码。。。
      *mobileEFF({obj}, {put, call}) {
          Toast.loading('加载中...', 3);
          const result = yield call(postMobileApi, obj);
          if (result.code == 200) {
              // yield put({ type: 'save', data: {Ret:result.ret||[]} });
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5, () => {
                  // this.props.dispatch(routerRedux.push({
                  //     pathname: `/login`,
                  // }));
              });
          }

          return result;
      },

      // 忘记密码。。。验证手机验证码
      *mobileVerifyEFF({ obj }, {put, call}) {
          Toast.loading('加载中...', 3);
          const result = yield call(mobileApi, obj);
          return result;
      },

      // 忘记密码。。。获取手机验证码
      *getMobileVerifyEFF({ obj }, {put, call}) {
          Toast.loading('加载中...', 3);
          const result = yield call(getMobileVerifyApi, obj);
          if (result.code == 200) {
              // yield put({ type: 'save', data: {Ret:result.ret||[]} });
              Toast.hide();
          } else {
              Toast.fail(result.msg, 1.5, () => {
                  // this.props.dispatch(routerRedux.push({
                  //     pathname: `/login`,
                  // }));
              });
          }

          return result;
      },
  },
  subscriptions: {
    setup({dispatch,history}){
      return history.listen(({pathname,query})=>{
        // console.log(pathname)
        if(pathname=='/userProtocol'){
         dispatch({ type:'UserProtocolEFF' });
        }
      })
    }
  },
};
